package cn.tedu.boot41.Controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        System.out.println(picFile);

        //得到文件的原始文件
        String filename = picFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));

        filename=UUID.randomUUID()+suffix;
        System.out.println("新文件名："+filename);

        //保存新的文件夹
//        String dirPath="D:\\图片\\高清图片";
        File file = new File(dirPath);
        //不存在则创建
        if(!file.exists()){
            file.mkdirs();
        }
        //得到文件的完整路径
        String filePath=dirPath+"/"+filename;
        picFile.transferTo(new File(filePath));
        return filename;
    }

    @RequestMapping("/remove")
    public void remove(String filename){
        System.out.println("删除的文件是："+filename);
        String filePath=dirPath+"/"+filename;
        new File(filePath).delete();
        System.out.println("删除的文件完整路径为："+filePath);
    }
}
